package com.xkdx.caipiao.presistence.login;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.HomePageActivity;
import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.WelcomeActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.my.RegisterActivity;
import com.xkdx.caipiao.presistence.findpsw.FindPwsActivity;
import com.xkdx.caipiao.presistence.welcome.WelcomeAction;
import com.xkdx.caipiao.presistence.welcome.WelcomePresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;
import com.xkdx.caipiao.util.Tost;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class Login extends ThreadActivity {
	private WelcomeAction action1;
	private WelcomePresistence presistence1;
	private String devicetype = "2";
	private String platform = "xkdx";
	private String version= "2014.10.20";
	private String devicetoken;
	private String puid = "1";

	ImageView back;
	Button login;
	EditText name, psd;

	LoginAction action;
	public static LoginModule module;
	LoginPresistence presistence;

	TextView foget_password, titel_info;
	private String names;
	private String psds;
	private Pattern p;
	private Matcher m;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_lottery_caipiao);
		TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(WelcomeActivity.TELEPHONY_SERVICE);
		devicetoken = mTelephonyManager.getDeviceId();
		findView();
		setOnClick();
	}

	private void setOnClick() {
		// 注册

		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(TextUtils.isEmpty(name.getText().toString())){
					Tost.toast(Login.this, "账号不能为空", null);
					//Toast.makeText(Login.this, "账号不能为空", 1).show();
					return;
				}else if(name.getText().toString().length()<11){
					Toast.makeText(Login.this, "请输入正确的手机号", 1).show();
					return;
				}
				if (TextUtils.isEmpty(psd.getText().toString())) {
					Toast.makeText(Login.this, "密码不能为空", 1).show();
					return;
				}else if (psd.getText().toString().length() < 6 ) {
					Toast.makeText(Login.this, "密码输入错误,请重新输入", 1).show();
					return;
				}
				startThread();
			}
		});
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharePrefenceUtil sp = new SharePrefenceUtil(Login.this, SharePrefenceUtil.USERINFO);
				sp.clear();
				Intent intent = new Intent(Login.this, HomePageActivity.class);
				startActivity(intent);
			}
		});

		foget_password.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						FindPwsActivity.class);
				startActivity(intent);

			}
		});
	}

	private void findView() {
		name = (EditText) findViewById(R.id.name);
		psd = (EditText) findViewById(R.id.pwd);
		back = (ImageView) findViewById(R.id.back);
		login = (Button) findViewById(R.id.btn_login);
		foget_password = (TextView) findViewById(R.id.foget_password);
		titel_info = (TextView) findViewById(R.id.title_info);
		titel_info.setText("注册");
		titel_info.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Login.this, RegisterActivity.class);
				startActivityForResult(intent, 300);
			}
		});

	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			setResult(200);
			IConstants.FromRadioButton = 1;
			puid = module.loginInfo.getId();
			startThread1();
			finish();
			
			
		}
		else{
			if ("5006".equals(module.code)) {
				Toast.makeText(this, "账号不存在！", Toast.LENGTH_SHORT).show();
				return;
			}
			if ("5007".equals(module.code)) {
				Toast.makeText(this, "账号或者密码错误！", Toast.LENGTH_SHORT).show();
				return;
			}
			}
	
		}
	

	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		String md5 = name.getText().toString()+psd.getText().toString()+IConstants.key;
		String signcode = Md5Utils.md5(md5);
		
		action = new LoginAction(name.getText().toString(), psd.getText().toString(),signcode);
		module = new LoginModule(Login.this);
		presistence = new LoginPresistence(Login.this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();
	}
	public void startThread1() {
		action1 = new WelcomeAction(devicetype,platform,version,
				devicetoken,puid);
		presistence1 = new WelcomePresistence(this);
		presistence1.setActitons(action1);
		presistence1.execute();


	}
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {

		super.onActivityResult(arg0, arg1, arg2);
		if (arg1 == 301) {
			IConstants.FromRadioButton = 1;
			finish();
		}
	}

	@Override
	public void onBackClick(View view) {
		finish();
	}
}
